package com.thinkgsp.webapp.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgsp.jxc.model.Dept;
import com.thinkgsp.sys.model.SysResource;
import com.thinkgsp.sys.model.SysUser;
import com.thinkgsp.sys.service.SysResourceService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExtReturn;

@Controller
@RequestMapping(value = "/menu")
public class SysResourceController extends BaseController {
	@Autowired
	private SysResourceService sysResourceService;
	
	@RequestMapping("/menuroot.do")
    @ResponseBody
    public Object menuroot(HttpServletRequest request,
			HttpServletResponse response){
		List<SysResource> res = sysResourceService.getAllRootResource();
		return res;
	}
	@RequestMapping("/menuchild.do")
    @ResponseBody
    public Object menuchild(HttpServletRequest request,
			HttpServletResponse response){
		SysUser user = (SysUser) SecurityContextHolder.getContext()
			    .getAuthentication()
			    .getPrincipal();
		int parentId = Integer.parseInt(request.getParameter("id"));
		List<SysResource> res = sysResourceService.getUserResource(user.getUserId(), parentId);
		return res;
	}
	@RequestMapping("/menuall.do")
    @ResponseBody
    public Object menuall(HttpServletRequest request,
			HttpServletResponse response){
		List<SysResource> res = sysResourceService.getAllChildResource();
		return res;
	}
	@RequestMapping("/getuser.do")
    @ResponseBody
    public Object getuser(HttpServletRequest request,
			HttpServletResponse response){
		SysUser user =null;
		user = (SysUser) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
		return user;
	}
	@RequestMapping("/setuserinv.do")
    @ResponseBody
    public Object setuserinv(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String idstr=request.getParameter("id");
			if(StringUtils.isBlank(idstr))
				return new ExtReturn(false,"未获取操作货位的相关信息");
			SysUser user = (SysUser) SecurityContextHolder.getContext()
					    .getAuthentication()
					    .getPrincipal();
			if(user==null)
				return new ExtReturn(true,"用户未登录或登录超时");
			Integer currentInvAdd=Integer.decode(idstr);
			
			user.setCurrentInvAdd(currentInvAdd);
			return new ExtReturn(true,"选择操作货位成功");
		} catch (Exception e) {
			return new ExtReturn(false,"选择操作货位失败",e.getMessage());
		}
	}
	
	@RequestMapping("/setUserLoginDept.do")
    @ResponseBody
    public Object setUserLoginDept(Model model ,@RequestBody Dept logonDept){
		try {

			SysUser user = (SysUser) SecurityContextHolder.getContext()
					    .getAuthentication()
					    .getPrincipal();
			if(user==null)
				return new ExtReturn(true,"用户未登录或登录超时");
			user.setLogonDept(logonDept);
			return new ExtReturn(true,"变更登录部门成功");
		} catch (Exception e) {
			return new ExtReturn(false,"变更登录部门失败",e.getMessage());
		}
	}
}
